<!DOCTYPE html>
<html>
<head>
<title>SIMULATION</title>
<link rel="stylesheet" type="text/css" href="xschem_man.css" />
<style type="text/css">
/* Local styling goes here */
p{padding: 15px  30px 10px;}

</style>

</head>
<body>

<!-- start of slide -->
<div class="content">   
<!-- navigation buttons -->
<a href="net_probes.html" class="prev">PREV</a>
<a href="xschem_man.html" class="home">UP</a>
<a href="developer_info.html" class="next">NEXT</a>
 <!-- slide title -->
 <h1>SIMULATION</h1><br>
 <p>
  One of the design goals of XSCHEM is the ability to launch a simulation without additional manual file editing. 
  For this purpose XSCHEM stores in a schematic not only the circuit but also the simulator
  settings and the additional files that are needed.
  For example there is a <kbd>devices/netlist.sym</kbd> and <kbd>devices/netlist_not_shown.sym</kbd> symbol that
  can be placed in a schematic acting as a container of text files for all the needed SPICE models and any
  additional information to make the schematic ready for simulation.
 </p>
 <img src="simulation10.png">
 <p>
  The <kbd>devices/netlist_not_shown</kbd> symbol shown in the picture (with name MODELS) for example contains
  all the spice models of the components used in the schematic, this makes
  the schematic self contained, no additional files are needed to run a simulation.
  After generating the netlist (for example poweramp.spice) the resulting SPICE netlist can be sent directly for
  simulation (for example <kbd>hspice -i poweramp.spice</kbd> for the  Hspice(TM) simulator). 
 </p>

 <h3> VERILOG SIMULATION</h3>
 <p> This is a tutorial showing how to run a simulation with XSCHEM. The first important thing to
  note is that XSCHEM is just a schematic editor, so we need to setup valid bindings to
  simulators. For this tutorial we plan to do a <kbd>Verilog</kbd>
  simulation since there is a very good open source simulator available, called
  <a href="http://iverilog.icarus.com">Icarus Verilog</a>.
  There is also a good waveform viewer called 
  <a href="https://sourceforge.net/projects/gtkwave">gtkwave</a>
  that is able to show simulator results. Install these two valuable tools and setup
  simulator invocation by using the Simulator configurator (<kbd>Simulation-&gt;Configure Simulators and tools</kbd>).
 </p>
 <img src="simulation11.png">
 <p>
  The text entry on the verilog line is the command to invoke icarus verilog simulation. <kbd>$N</kbd>
  will be expanded to the netlist file (<kbd>$netlist_dir/greycnt.v</kbd>), while <kbd>$n</kbd>
  will be replaced with the circuit name without extension (<kbd>$netlist_dir/greycnt</kbd>).
  Note also the command to invoke gtkwave on the vcd file generated by theverilog simulation.
  If <kbd>Save Configuration</kbd> button is pressed the changes are made permanent by saving 
  in a <kbd>~/.xschem/simrc</kbd> file.
 </p>
 <p>
  In the XSCHEM distribution there is one example design, <kbd>examples/greycnt.sch</kbd>.<br>
  Load this design: 
 </p>
 <pre class="code">
user:~$ xschem examples/greycnt
 </pre>
 <img src="simulation1.png">
 <p>
  This testbench has a 8 bit input vector A[7:0] and two output vectors, B[7:0] and C[7:0].
  B[7:0] is a grey coded vector, this mean that if A[7:0] is incremented as a binary number
  B[7:0] will increment by changing only one bit at a time. The C[7:0] vector is the reverse transformation
  from grey-code to binary, so at the end if simulation goes well C[7:0] == A[7:0].
  In this schematic there are some components, the first one is the <kbd>xnor</kbd> gate, the second one is
  the <kbd>assign</kbd> element. The 'xnor' performs the logical 'Not-Xor' of its inputs, while 'assign'
  just propagates the input unchanged to the output, optionally with some delay.
  This is useful if we want to change the name of a net
  (putting two labels with different names on the same net is not allowed, since this is normally an error, 
  leading to a short circuit).
 </p>
 <p>
  An Ex-Nor gate can be represented as a verilog primitive, so for the xnor gate we just need to setup 
  a <kbd>verilog_format</kbd> attribute in the global property string of the <kbd>xnor.sym</kbd> gate: 
 </p>
 <img src="simulation2.png">
 <p>
  the 'assign' symbol is much simpler, in this property string you see the definition for SPICE
  (<kbd>format</kbd> attribute), Verilog (<kbd>verilog_format</kbd>) and VHDL (<kbd>vhdl_format</kbd>).
   This shows how a single symbol can be used for different netlist formats.
 </p>
 <img src="simulation3.png">
 <p>
  While showing the top-level testbench <kbd>greycnt</kbd> set XSCHEM in Verilog mode
  (menu <kbd>Options-&gt;Verilog</kbd> radio button, or <kbd>&lt;Shift&gt;V</kbd> key) and press
  the edit property <kbd>'q'</kbd> key, you will see some verilog code:
 </p>
 <img src="simulation4.png">
 <p> 
  This is the testbench behavioral code that generates stimuli for the simulation and gives
  instructions on where to save simulation results. If you generate the verilog netlist with 
  the <kbd>Netlist</kbd> button on the right side of the menu bar (or <kbd>&lt;Shift&gt;N</kbd>
  key) a <kbd>greycnt.v</kbd> file will be generated in the simulation directory
  (<kbd>${HOME}/xschem_library/simulations</kbd> is the default path in the XSCHEM distribution, but
  can be changed with the <kbd>set netlist_dir $env(HOME)/simulations</kbd> in <kbd>xschemrc</kbd> file):
 </p>
 <pre class="code">
`timescale 1ps/1ps
module greycnt (
  output wire [7:0] B,
  output wire [7:0] C
);

reg [7:0] A  ;

xnor #(1 , 1 ) x2 ( B[4] , A[5] , A[4] ); 
xnor #(1 , 1 ) x3 ( B[5] , A[6] , A[5] ); 
xnor #(1 , 1 ) x14 ( B[6] , A[7] , A[6] ); 
assign #1 B[7] = A[7] ; 
xnor #(1 , 1 ) x1 ( B[1] , A[2] , A[1] ); 
xnor #(1 , 1 ) x4 ( B[2] , A[3] , A[2] ); 
xnor #(1 , 1 ) x5 ( B[3] , A[4] , A[3] ); 
xnor #(1 , 1 ) x6 ( B[0] , A[1] , A[0] ); 
xnor #(1 , 1 ) x7 ( C[4] , C[5] , B[4] ); 
xnor #(1 , 1 ) x8 ( C[5] , C[6] , B[5] ); 
xnor #(1 , 1 ) x9 ( C[6] , C[7] , B[6] ); 
assign #1 C[7] = B[7] ; 
xnor #(1 , 1 ) x10 ( C[1] , C[2] , B[1] ); 
xnor #(1 , 1 ) x11 ( C[2] , C[3] , B[2] ); 
xnor #(1 , 1 ) x12 ( C[3] , C[4] , B[3] ); 
xnor #(1 , 1 ) x13 ( C[0] , C[1] , B[0] ); 
initial begin
  $dumpfile("dumpfile.vcd");
  $dumpvars;
  A=0;
end

always begin
  #1000;
  $display("%08b %08b", A, B);
  A=A + 1;
  if(A==0) $finish;
end
endmodule
 </pre>
 <p>
  you will recognize the behavioral code right after the netlist specifying the connection of
  nets to the xnor and assign gates and all the necessary verilog declarations.
  If you press the <kbd>Simulation</kbd> button the <kbd>Icarus Verilog</kbd> simulator will be executed
  to compile (iverilog) and run (vvp) the simulation, a terminal window will show the simulation output,
  in this case the input vector A[7:0] and the grey coded B[7:0] vectors are shown. You can quit the
  simulator log window by pressing <kbd>'q'</kbd>.
 </p>
 <img src="simulation5.png">
 <p>
  If simulation completes with no errors waveforms can be viewed. Press the <kbd>Waves</kbd> button
  in the top-right of the menu bar, you may add waveforms in the gtkwave window:
 </p>
 <img src="simulation6.png">
 <p>
  If the schematic contains errors that the simulator can not handle instead of the simulation
  log a window showing the error messages from the simulator is shown: 
 </p>
 <img src="simulation7.png">
 <p>
  To facilitate the debug you may wish to edit the netlist (<kbd>Simulation-&gt;Edit Netlist</kbd>)
  to locate the error, in the picture below i inserted deliberately a random string to
  trigger the failure:
 </p>
 <img src="simulation8.png">
 <p>
  As you can see the error is in the behavioral code of the top level greycnt schematic, so edit the 
  global property (<kbd>'q'</kbd> key with no component selected) and fix the error.
 </p>
 <img src="simulation9.png">

 <!-- end of slide -->
 <div class="filler"></div>
</div>

<!-- frame footer -->
<iframe seamless src="xschem_footer.html"  class="footer_iframe" >
</body>
</html>

